// pages/rank/rank.js
import { formatDate,getRankList,toTemp} from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    show: false,
    minDate: new Date(2023, 0, 1).getTime(),
    maxDate: new Date().getTime(),    
    active:0,
    dateRange:[],
    start_time:'',
    end_time:'',
    name:'b',
    rankList:[]
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  onConfirm(event) {
    const [start, end] = event.detail;    
    this.setData({
      dateRange:event.detail,
      show: false,
      active:-1,
      date: `${formatDate(start,1)} - ${formatDate(end,1)}`,
      start_time:formatDate(start,1),
      end_time:formatDate(end,1)
    });
    this.getData(this.data.name)
  },
  chnageDate(e){
    if(e.currentTarget.id==this.data.active){
      return;
    }
     let currentDate=[]
     let start=''
     let end=''
     let dataVal=''
     const date = new Date();   
    switch(e.currentTarget.id){
      case '0':
        currentDate=[];
        start=''
        end=''
        dataVal=''
        break;
      case '1':
        let before = date.getTime() - 7 * 86400 * 1000;         
        let beforeDate = new Date(before);
        currentDate = [toTemp(beforeDate),toTemp(date)];
        start=formatDate(beforeDate,1)
        end=formatDate(date,1)
        dataVal=start+'-'+end
        break;
      case '2':
        let year = date.getFullYear();  
        let month = date.getMonth() + 1;
        let monthDate = new Date(year + '-' + month + '-01');
        currentDate = [toTemp(monthDate),toTemp(date)];   
        start=formatDate(monthDate,1)
        end=formatDate(date,1)
        dataVal=start+'-'+end
        break;
    }
    this.setData({
      active:e.currentTarget.id,
      dateRange:currentDate,
      date: dataVal,
      start_time:start,
      end_time:end
    })   
    this.getData(this.data.name)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onChange(event){
    this.setData({
      name:event.detail.name,
      active:0,
      date:'',
      dateRange:[],
      start_time:'',
      end_time:'',
    })
    this.getData(this.data.name)
  },
  getData(name){
    let params={
      start_time:this.data.start_time,
      end_time:this.data.end_time
    }
    getRankList(name,params).then(res=>{
      this.setData({
        rankList:res.data
      })
    })
  },
  onLoad(options) {
    this.getData(this.data.name)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getData(this.data.name)
    setTimeout(()=>{
      wx.stopPullDownRefresh()
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})